<?php
session_start();
include('config/conn.php');

// 判断用户是否已登录
$is_logged_in = isset($_SESSION['username']) && !empty($_SESSION['username']);
?>



<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>成年人游戏平台</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            justify-content: flex-start;
            align-items: center;
            height: 100vh;
        }
        .container {
            width: 80%;
            max-width: 1200px;
            margin-top: 20px;
            text-align: center;
        }
        h1 {
            font-size: 32px;
            color: #333;
            margin-bottom: 40px;
        }
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        .game-card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            cursor: pointer;
            transition: transform 0.3s ease;
        }
        .game-card:hover {
            transform: translateY(-5px);
        }
        .game-card img {
            width: 100%;
            height: 150px;
            object-fit: cover;
        }
        .game-card h3 {
            padding: 10px;
            font-size: 18px;
            color: #333;
            background-color: #f9f9f9;
            margin: 0;
        }
        .footer {
            margin-top: 30px;
            font-size: 14px;
            color: #777;
        }
        /* Header styles */
        .header {
            width: 100%;
            background-color: #333;
            color: white;
            padding: 10px 0;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
            box-sizing: border-box;
        }
        .header h2 {
            margin: 0;
            font-size: 24px;
        }
        .header .user-info {
            display: flex;
            align-items: center;
        }
        .header .user-info a {
            color: white;
            text-decoration: none;
            margin-left: 15px;
        }
        .header .user-info a:hover {
            text-decoration: underline;
        }
        /* 登录框样式 */
        .login-box {
            background-color: white;
            padding: 10px 15px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
        }
        .login-box label {
            margin-right: 10px;
            font-size: 14px;
        }
        .login-box .input-group {
            display: flex;
            margin-bottom: 10px;
            width: 100%;
        }
        .login-box .input-group input {
            flex: 1;
            padding: 5px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
        }
        .login-box .input-group button {
            padding: 5px 10px;
            background-color: #333;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            margin-left: 10px;
        }
        .login-box .input-group button:hover {
            background-color: #444;
        }
        .login-box a {
            font-size: 12px;
            color: #333;
            text-decoration: none;
        }
        /* 隐藏登录框 */
        #loginModal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            justify-content: center;
            align-items: center;
            z-index: 9999;
        }
        #loginModal .modal-content {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
            text-align: center;
        }
        #loginModal .modal-content button.close {
            background-color: red;
            color: white;
            border: none;
            padding: 5px 10px;
            font-size: 14px;
            cursor: pointer;
        }
        #loginModal .modal-content button.close:hover {
            background-color: #d9534f;
        }
    </style>
</head>
<body>

    <!-- Header section -->
    <div class="header">
        <h2>               &emsp;&emsp; 18静游戏平台</h2>
        <div class="user-info">
            <?php if ($is_logged_in): ?>
                <span>欢迎，<?php echo htmlspecialchars($_SESSION['username']); ?></span>
                <span>当前会员等级：【
<?php           
               $uid= htmlspecialchars($_SESSION['uid']);
               
$usql = "SELECT * FROM user WHERE id = $uid ";  // 查询用户权限
$uresult = $conn->query($usql);
    $urow = $uresult->fetch_assoc();
           if ($uresult->num_rows > 0) {    
                if ($urow['vip']=='1') {
                    echo '免注册畅玩';
                }elseif ($urow['vip']=='2') {
                    echo '注册会员';
                }elseif ($urow['vip']=='3') {
                    echo '青铜会员';
                }elseif ($urow['vip']=='4') {
                    echo '铂金会员';
                }elseif ($urow['vip']=='5') {
                    echo '钻石会员';
                } }?>】</span>
                <a href="profile.php">充值会员</a>
                <a href="logout.php">退出登录</a>
            <?php else: ?>
                <!-- 登录按钮 -->
                <button onclick="openLoginModal()">登录</button>
            <?php endif; ?>
        </div>
    </div>

    <!-- Modal: 登录框 -->
    <div id="loginModal" class="modal">
        <div class="modal-content">
            <button class="close" onclick="closeLoginModal()">×</button>
            <form action="login_process.php" method="POST">
                <div class="input-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="username" name="username" placeholder="用户名" required>
                </div>
                <div class="input-group">
                    <label for="password">密&emsp;码：</label>
                    <input type="password" id="password" name="password" placeholder="密码" required>
                </div><br>
                <div class="input-group">
                   <a href="user/register.php">注册</a> &emsp;&emsp; <button type="submit">登录</button> 
                </div>
            </form>

        </div>
    </div>

    <!-- Main content section -->
    <div class="container">
        <h1>18🈲游戏平台，如果您未满18岁请关闭该网页！</h1>
        <div class="grid">
            <?php 
            if ($game->num_rows > 0):
                while($row = $game->fetch_assoc()):
                    // 先获取所有需要的变量
                    $yxid = $row["id"];
                    $yxname = $row["name"];
                    $yxms = $row["ms"];
                    $yxvip = $row["vip"];
                    $yximg = $row["img"];
                    $yxurl = $row["url"];
                    $yxcny = $row["cny"];
                    $yxtime = $row["time"];
            ?>
                    <div class="game-card" onclick="window.location.href='<?php 
                        if($yxvip == '1') {
                            echo htmlspecialchars($yxurl);
                        } else {
                            echo htmlspecialchars('/user/rz.php?yxid=' . $yxid);
                        }
                    ?>'">
                        <img src="<?php echo htmlspecialchars($yximg); ?>" alt="<?php echo htmlspecialchars($yxms); ?>">
                        <h3><?php echo htmlspecialchars($yxname); ?></h3>
                        <p>
                            <?php
                            if ($yxvip == '6') {
                                echo '$', htmlspecialchars($yxcny);
                            } else {
                                $vipTypes = [
                                    '1' => '免注册畅玩',
                                    '2' => '注册会员',
                                    '3' => '青铜会员',
                                    '4' => '铂金会员',
                                    '5' => '钻石会员'
                                ];
                                echo isset($vipTypes[$yxvip]) ? htmlspecialchars($vipTypes[$yxvip]) : '';
                            }
                            ?>
                        </p>
                    </div>
            <?php 
                endwhile;
            endif;
            ?>
        </div>
        <p class="footer">选择一个游戏开始玩吧！</p>
    </div>

    <script>
        // 打开登录框
        function openLoginModal() {
            document.getElementById("loginModal").style.display = "flex";
        }

        // 关闭登录框
        function closeLoginModal() {
            document.getElementById("loginModal").style.display = "none";
        }

        // 点击登录框外部区域也关闭登录框
        window.onclick = function(event) {
            var modal = document.getElementById("loginModal");
            if (event.target == modal) {
                closeLoginModal();
            }
        }
    </script>
</body>
</html>
